/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Configuracoes;

/**
 *
 * @author Thiago
 */
public class telaConfiguracao extends javax.swing.JFrame {

    private Activity Pai;
    private Configuracoes config;

    public telaConfiguracao(Activity pai) throws IOException {
        initComponents();
        this.Pai = pai;
        this.Pai.setEnabled(false);
        this.setVisible(true);
        this.setLocationRelativeTo(null);

        this.textoNPM.setText("" + this.valorNPM.getValue());
        this.textoEMM.setText("" + this.valorEMM.getValue());
        this.textoAMP.setText("" + this.valorAMP.getValue());

        this.config = Configuracoes.getInstance();
        carregarConfiguracoes();
    }

    private void carregarConfiguracoes() throws IOException {
        int NPM, EMM, AMP;
        this.config.openConfiguracoes();
        NPM = this.config.getNumeroPontos();
        EMM = this.config.getDimensaoTerreno();
        AMP = this.config.getAlturaTerreno();
        this.textoNPM.setText("" + NPM);
        this.valorNPM.setValue(NPM);
        this.textoEMM.setText("" + EMM);
        this.valorEMM.setValue(EMM);
        this.textoAMP.setText("" + AMP);
        this.valorAMP.setValue(AMP);
    }

    private void salvarConfiguracoes() throws IOException {
        int NPM, EMM, AMP;
        NPM = this.valorNPM.getValue();
        EMM = this.valorEMM.getValue();
        AMP = this.valorAMP.getValue();
        this.config.setNumeroPontos(NPM);
        this.config.setDimensaoTerreno(EMM);
        this.config.setAlturaTerreno(AMP);
        this.config.saveConfiguracoes();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        valorNPM = new javax.swing.JSlider();
        textoNPM = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        valorEMM = new javax.swing.JSlider();
        textoEMM = new javax.swing.JLabel();
        textoAMP = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        valorAMP = new javax.swing.JSlider();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(435, 200));
        setPreferredSize(new java.awt.Dimension(435, 200));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(null);

        jLabel1.setText("Numero de pontos da malha:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 23, 138, 14);

        valorNPM.setMaximum(500);
        valorNPM.setMinimum(200);
        valorNPM.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                valorNPMStateChanged(evt);
            }
        });
        getContentPane().add(valorNPM);
        valorNPM.setBounds(160, 20, 200, 23);

        textoNPM.setText("jLabel2");
        getContentPane().add(textoNPM);
        textoNPM.setBounds(370, 20, 34, 14);

        jLabel2.setText("Extensão máxima da malha:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 55, 134, 14);

        valorEMM.setMaximum(1000);
        valorEMM.setMinimum(500);
        valorEMM.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                valorEMMStateChanged(evt);
            }
        });
        getContentPane().add(valorEMM);
        valorEMM.setBounds(160, 50, 200, 23);

        textoEMM.setText("jLabel2");
        getContentPane().add(textoEMM);
        textoEMM.setBounds(370, 50, 34, 14);

        textoAMP.setText("jLabel2");
        getContentPane().add(textoAMP);
        textoAMP.setBounds(370, 80, 34, 14);

        jLabel3.setText("Altura máxima do ponto:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 90, 118, 14);

        valorAMP.setValue(0);
        valorAMP.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                valorAMPStateChanged(evt);
            }
        });
        getContentPane().add(valorAMP);
        valorAMP.setBounds(160, 80, 200, 23);

        jButton1.setText("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(270, 130, 63, 23);

        jButton2.setText("Voltar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(350, 130, 61, 23);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void valorNPMStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_valorNPMStateChanged
        this.textoNPM.setText("" + this.valorNPM.getValue());
    }//GEN-LAST:event_valorNPMStateChanged

    private void valorEMMStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_valorEMMStateChanged
        this.textoEMM.setText("" + this.valorEMM.getValue());
    }//GEN-LAST:event_valorEMMStateChanged

    private void valorAMPStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_valorAMPStateChanged
        this.textoAMP.setText("" + this.valorAMP.getValue());
    }//GEN-LAST:event_valorAMPStateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        this.Pai.setEnabled(true);
        this.Pai.setVisible(true);
    }//GEN-LAST:event_formWindowClosed

    @SuppressWarnings("empty-statement")
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            salvarConfiguracoes();
        } catch (IOException exception) {
        }
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        try {
            salvarConfiguracoes();
        } catch (IOException exception) {
        }
        this.dispose();
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel textoAMP;
    private javax.swing.JLabel textoEMM;
    private javax.swing.JLabel textoNPM;
    private javax.swing.JSlider valorAMP;
    private javax.swing.JSlider valorEMM;
    private javax.swing.JSlider valorNPM;
    // End of variables declaration//GEN-END:variables
}
